package com.xinou.daily_report.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinou.common.base.config.ApiResponse;
import com.xinou.common.util.BeanUtil;
import com.xinou.daily_report.PO.WeekPlanPO;
import com.xinou.daily_report.PO.WeekResultPO;
import com.xinou.daily_report.PO.WeekResultUpdatePO;
import com.xinou.daily_report.VO.WeekPlanVO;
import com.xinou.daily_report.VO.WeekResultInfoVO;
import com.xinou.daily_report.VO.WeekResultVO;
import com.xinou.daily_report.entity.SysWeekPlan;
import com.xinou.daily_report.entity.SysWeekResult;
import com.xinou.daily_report.entity.SysWeekTime;
import com.xinou.daily_report.service.SysWeekPlanService;
import com.xinou.daily_report.service.SysWeekResultService;
import com.xinou.daily_report.service.SysWeekTimeService;
import com.xinou.sso.util.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-06-11
 */
@RestController
@RequestMapping("/mini/week-result")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = {"前台周结果"})
public class MiniWeekResultController {

    private final SysWeekResultService sysWeekResultService;
    private final SysWeekPlanService sysWeekPlanService;
    private final SysWeekTimeService sysWeekTimeService;

    @PostMapping("/list")
    @ApiOperation("获取和筛选周报结果列表")
    @RequiresPermissions({"mini:week-result:list"})
    public ApiResponse<WeekResultInfoVO> weekResultList(@RequestBody WeekResultPO weekResultPO){
                System.out.println(weekResultPO);

        return sysWeekResultService.weekResultList(weekResultPO);
    }

    @PostMapping("/save")
    @ApiOperation("添加周报结果")
    @RequiresPermissions({"mini:week-result:save"})
    public ApiResponse<Void> weekResultSave(@RequestBody WeekResultUpdatePO weekResultUpdatePO){
        System.out.println(weekResultUpdatePO);
        return sysWeekResultService.weekResultSave(weekResultUpdatePO);
    }

    @PostMapping("/update")
    @ApiOperation("修改周报结果")
    @RequiresPermissions({"mini:week-result:update"})
    public ApiResponse<Void> weekResultUpdate(@RequestBody WeekResultUpdatePO weekResultUpdatePO){
        return sysWeekResultService.weekResultUpdate(weekResultUpdatePO);
    }


    @PostMapping("/delete")
    @ApiOperation("删除周报结果")
    @RequiresPermissions({"mini:week-result:delete"})
    public ApiResponse<Void> weekResultDelete(@RequestBody WeekResultPO weekResultPO){
        return sysWeekResultService.weekResultDelete(weekResultPO);
    }

    @PostMapping("/getById")
    @ApiOperation("根据id查询周报结果")
    @RequiresPermissions({"mini:week-result:getById"})
    public ApiResponse<WeekResultInfoVO> weekResultGetById(@RequestBody WeekResultPO weekResultPO){
        return sysWeekResultService.weekResultGetById(weekResultPO);
    }

    @PostMapping("/getDefaults")
    @ApiOperation("查询周结果默认值")
//    @RequiresPermissions({"admin:weekResult:getDefaults"})
    public ApiResponse<WeekResultInfoVO> weekResultGetDefaults(){
        LocalDateTime now = LocalDateTime.now();
        SysWeekTime sysWeekTime = sysWeekTimeService.getOne(new LambdaQueryWrapper<SysWeekTime>()
                .le( SysWeekTime::getBeginTime, now)
                .ge( SysWeekTime::getEndTime, now));
        if (sysWeekTime == null) {
            return new ApiResponse<>(Collections.emptyList());
        }
        Long userId = ShiroUtil.getUserId();
        Long weekId = sysWeekTime.getId();
        LambdaQueryWrapper<SysWeekResult> resultLambdaQueryWrapper = new LambdaQueryWrapper<SysWeekResult>()
                .eq(SysWeekResult::getUserId, userId)
                .eq(SysWeekResult::getWeekId, weekId)
                .orderByDesc(SysWeekResult::getInsertTime);
        int resultCount = sysWeekResultService.count(resultLambdaQueryWrapper);
        if (resultCount != 0) {
            List<SysWeekResult> list = sysWeekResultService.list(resultLambdaQueryWrapper);
            WeekResultInfoVO weekResultInfoVO = new WeekResultInfoVO();
            weekResultInfoVO.setWeekResultInfoVOList(BeanUtil.listA2ListB(list, WeekResultInfoVO.class));
            weekResultInfoVO.setBeginTime(sysWeekTime.getBeginTime());
            weekResultInfoVO.setEndTime(sysWeekTime.getEndTime());
            return new ApiResponse<>(Collections.singleton(weekResultInfoVO));
        }
        LambdaQueryWrapper<SysWeekPlan> planLambdaQueryWrapper = new LambdaQueryWrapper<SysWeekPlan>()
                .eq(SysWeekPlan::getUserId, userId)
                .eq(SysWeekPlan::getWeekId, weekId)
                .orderByDesc(SysWeekPlan::getInsertTime);
        int planCount = sysWeekPlanService.count(planLambdaQueryWrapper);
        if (planCount == 0) {
            return new ApiResponse<>(Collections.emptyList());
        }
        List<SysWeekPlan> list = sysWeekPlanService.list(planLambdaQueryWrapper);
        WeekResultInfoVO weekResultInfoVO = new WeekResultInfoVO();
        List<WeekResultInfoVO> vos = new ArrayList<>();
        WeekResultInfoVO vo;
        for (SysWeekPlan sysWeekPlan : list) {
            vo = new WeekResultInfoVO();
            vo.setPromise(sysWeekPlan.getKeyWork());
            vo.setComplete(sysWeekPlan.getExpectedResult());
            vo.setResultType(0);
            vos.add(vo);
        }
        weekResultInfoVO.setWeekResultInfoVOList(vos);
        weekResultInfoVO.setBeginTime(sysWeekTime.getBeginTime());
        weekResultInfoVO.setEndTime(sysWeekTime.getEndTime());
        return new ApiResponse<>(Collections.singleton(weekResultInfoVO));
    }

}

